<?php

namespace console\controllers;

use Yii;
use yii\console\Controller;
use Exception;

/**
 *
 * Class SynController
 * @package console\controllers
 */
class SynController extends Controller
{
    /**
     * syn/patient-callback
     *
     * 同步患者回访时间
     */
    public function actionPatientCallback()
    {
        try {
            $startTime = time();

            Yii::$app->services->patientCallback->synPatientUserCallbackTime();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * syn/patient-degree-num
     *
     * 同步患者完善度
     */
    public function actionPatientDegreeNum()
    {
        try {
            $startTime = time();

            Yii::$app->services->patientUser->synPatientDegreeNum();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (Exception $e) {
            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }

    /**
     * syn/patient-body
     *
     * 同步患者部位
     */
    public function actionPatientBody()
    {
        try {
            $startTime = time();

            $transaction = Yii::$app->db->beginTransaction();

            Yii::$app->services->patientUser->synPatientBody();

            $transaction->commit();

            $endTime = time();
            $diffTime = $endTime - $startTime;

            echo "success".PHP_EOL;
            echo "开始时间: {$startTime}".PHP_EOL;
            echo "结束时间: {$endTime}".PHP_EOL;
            echo "本次耗时: {$diffTime}".PHP_EOL;
        } catch (Exception $e) {
            $transaction->rollBack();

            echo 'error: '.$e->getMessage().PHP_EOL;
        }
    }
}
